message(NOTICE "开始构建 openssl 模块")
# 检查构建目标是否存在
if(TARGET openssl-${OPENSSL_TAG})
    message(NOTICE "openssl 模块已存在 openssl-${OPENSSL_TAG}")
 return()
endif()
# 检查生成的文件是否存在
existsInstallFiles(
    ${MODULE_STAMP}/openssl/${OPENSSL_TAG}
    ${MODULE_INSTALL_DIR}/openssl/${OPENSSL_TAG}
    openssl-${OPENSSL_TAG})

# 构建配置文件
set(OPENSSL_CONFIGURE CACHE STRING "openssl configure" FORCE)
set(OPENSSL_ENV CACHE STRING "openssl build command" FORCE)


# Android 配置 模块
if(CMAKE_SYSTEM_NAME STREQUAL "Android")

# Android 配置 模块
list(APPEND OPENSSL_CONFIGURE "<SOURCE_DIR>/Configure")
# 编译器工具链
if("${ANDROID_ABI}" STREQUAL "armeabi-v7a")
    list(APPEND OPENSSL_CONFIGURE "android-arm")
elseif("${ANDROID_ABI}" STREQUAL "arm64-v8a")
    list(APPEND OPENSSL_CONFIGURE "android-arm64")
elseif("${ANDROID_ABI}" STREQUAL "x86")
    list(APPEND OPENSSL_CONFIGURE "android-x86")
elseif("${ANDROID_ABI}" STREQUAL "x86_64")
    list(APPEND OPENSSL_CONFIGURE "android-x86_64")
endif()
list(APPEND OPENSSL_CONFIGURE "-D__ANDROID_API__=${ANDROID_PLATFORM}")
# 构建命令
list(APPEND OPENSSL_ENV "env")
list(APPEND OPENSSL_ENV "ANDROID_NDK_ROOT=${CMAKE_FIND_ROOT_PATH}")
list(APPEND OPENSSL_ENV "PATH=${ANDROID_TOOLCHAIN_ROOT}/bin:$ENV{PATH}")

# Linux 配置 模块
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    list(APPEND OPENSSL_CONFIGURE "<SOURCE_DIR>/Configure")
# Windows 配置 模块
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    list(APPEND OPENSSL_CONFIGURE "<SOURCE_DIR>/Configure")
    list(APPEND OPENSSL_CONFIGURE "win64-x86_64")
else()
    message(FATAL_ERROR "不支持的系统类型 ${CMAKE_SYSTEM_NAME}")
endif()
if(BUILD_SHARED_LIBS)
    list(APPEND OPENSSL_CONFIGURE "shared")
endif()
if(BUILD_STATIC_LIBS)
    list(APPEND OPENSSL_CONFIGURE "no-shared")
endif()

# 构建 openssl 库
ExternalProject_Add(
    openssl-${OPENSSL_TAG}
    # 目录配置
    PREFIX         ${EXTERNAL_PROJECT_ROOT}                        # ✅ 显式设置 prefix 目录
    SOURCE_DIR     ${MODULE_SRC}/openssl/${OPENSSL_TAG}            # ✅ 显式设置 source 目录
    BINARY_DIR     ${MODULE_BUILD}/openssl/${OPENSSL_TAG}          # ✅ 显式设置 build 目录
    INSTALL_DIR    ${MODULE_INSTALL_DIR}/openssl/${OPENSSL_TAG}    # ✅ 显式设置 install 目录
    STAMP_DIR      ${MODULE_STAMP}/openssl/${OPENSSL_TAG}          # ✅ 显式设置 stamp 目录
    TMP_DIR        ${MODULE_TMP}/openssl/${OPENSSL_TAG}            # ✅ 显式设置 tmp 目录

    DOWNLOAD_COMMAND
        COMMAND ${CMAKE_COMMAND}
            -DSOURCE_DIR=<SOURCE_DIR>
            -DGIT_REPOSITORY=${OPENSSL_REPO}
            -DGIT_TAG=${OPENSSL_TAG}
            -P ${__DIR}/utils/git.cmake

    CONFIGURE_COMMAND
       ${OPENSSL_ENV}
       ${OPENSSL_CONFIGURE}
        --prefix=<INSTALL_DIR>
        --openssldir=<INSTALL_DIR>/ssl
        --libdir=lib
        no-unit-test
        no-async
        no-asm
        no-tests
        enable-ssl-trace
        no-ssl3
        no-comp
        no-tls1
        no-tls1_1
        no-weak-ssl-ciphers
        no-legacy
        no-external-tests
    # 构建命令
    BUILD_COMMAND
      ${OPENSSL_ENV}
      make -j${NPROC}
    # 安装命令
    INSTALL_COMMAND
        COMMAND make install_sw install_ssldirs
            # 将安装文件路径保存到 文件去
            COMMAND ${CMAKE_COMMAND}
            -DINSTALL_DIR=<INSTALL_DIR>
            -DCMAKE_PREFIX_PATH=${SYSTEM_PREFIX}
            -P ${__DIR}/utils/write_install_files.cmake
    # ✅ 启用所有日志（调试必备）
    LOG_DOWNLOAD      OFF                           # 下载日志
    LOG_CONFIGURE     OFF                            # 配置日志
    LOG_BUILD         OFF                           # 构建日志
    LOG_INSTALL       OFF                           # 安装日志
    LOG_OUTPUT_ON_FAILURE ON                       # 失败时自动输出日志
   # 依赖目标 安装需要先安装 automake libtool autopoint
   DEPENDS
   STEP_TARGETS configure build install # 关键：添加 install 步骤
)
# 创建 OpenSSL::SSL 目标
add_library(OpenSSL::SSL STATIC IMPORTED)
# 创建 OpenSSL::Crypto 目标
add_library(OpenSSL::Crypto STATIC IMPORTED)
# OpenSSL::SSL 属性设置
set_target_properties(OpenSSL::SSL PROPERTIES
VERSION ${OPENSSL_TAG}
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/openssl/${OPENSSL_TAG}/include
IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/openssl/${OPENSSL_TAG}/lib/libssl.a)
# OpenSSL::Crypto 属性设置
set_target_properties(OpenSSL::Crypto PROPERTIES
VERSION ${OPENSSL_TAG}
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
INTERFACE_INCLUDE_DIRECTORIES ${MODULE_INSTALL_DIR}/openssl/${OPENSSL_TAG}/include
IMPORTED_LOCATION ${MODULE_INSTALL_DIR}/openssl/${OPENSSL_TAG}/lib/libcrypto.a)
#依赖库
add_dependencies(OpenSSL::Crypto openssl-${OPENSSL_TAG})
add_dependencies(OpenSSL::SSL openssl-${OPENSSL_TAG})
